/* 
 * Vertex Shader for Enders game
 * Compile with:
 * cgc legs3.cg -o legs3.fb -entry main -profile arbvp1 
 */

struct vertex
{
	float4 position : POSITION;
    float4 normal   : NORMAL;
	float4 color0   : COLOR0;
	float2 bounds0  : TEXCOORD0;
	float2 bounds1	: TEXCOORD1;
	float2 bounds2	: TEXCOORD2;
	float2 bounds3	: TEXCOORD3;
	float2 bounds4	: TEXCOORD4;
	float2 bounds5	: TEXCOORD5;
};

struct fragment
{
	float4 position : POSITION;
	float4 color0   : COLOR0;
};

uniform float4x4 modelViewProj;

uniform float4 light;

//matrices for right lower leg movement
uniform float4x4 lowerRightMatrix;

//matrices for left lower leg movement
uniform float4x4 lowerLeftMatrix;

//matrices for right leg movement
uniform float4x4 RightMatrix;

//matrices for left leg movement
uniform float4x4 LeftMatrix;

//matrices for right lower arm
uniform float4x4 LowerRightArm;

//matrices for left lower arm
uniform float4x4 LowerLeftArm;

//matrices for right arm
uniform float4x4 RightArm;

//matrices for left arm
uniform float4x4 LeftArm;

//matrices for head
uniform float4x4 Head;

//matrices for torso
uniform float4x4 Torso;

fragment main(vertex IN)
{
	fragment OUT;
	
	float4x4 lastMatrix = modelViewProj;
	float4x4 totMatrix;
	float4x4 restMatrix = float4x4(1,0,0,0,
						   		   0,1,0,0,
						   		   0,0,1,0,
						   		   0,0,0,1);
	
	totMatrix = (
	 lowerRightMatrix * IN.bounds0[0] +
	 lowerLeftMatrix * IN.bounds0[1] +
	 RightMatrix * IN.bounds1[0] +
	 LeftMatrix * IN.bounds1[1] +
	 LowerRightArm * IN.bounds2[0] +
	 LowerLeftArm * IN.bounds2[1]);
		
	totMatrix = (totMatrix +
	 RightArm * IN.bounds3[0] +
	 LeftArm * IN.bounds3[1] +
	 Head * IN.bounds4[0] +
	 Torso * IN.bounds4[1] +
	 restMatrix * IN.bounds5[0]);
	
	float3 normal = mul((float3x3)totMatrix,(float3)IN.normal);
	
	lastMatrix = mul(lastMatrix, totMatrix);
	
	OUT.position = mul(lastMatrix,IN.position);
	float4 ambient = float4(0.3, 0.3, 0.3, 1.0);
	float4 colorA = float4(0.75, 0.75, 0.75, 1.0)*max(0.0, dot(light.xyz, normal));
	OUT.color0 = IN.color0*(ambient + colorA);

	return OUT;
}

technique twist {
  pass {
    VertexProgram = compile arbvp1 main();
  }
}